package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class BibliotecaApp {

    public static void main(String[] args) {
        Repository repository = buildLibrary();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        UserManager userManager = new UserManager();
        InputHandler inputHandler = new InputHandler(bufferedReader, userManager);
        String userActivity = null;
        createUsers(userManager);

        System.out.println("Welcome!");
        printBorrowables(repository.getAvailableBookList());

        while (true){

            System.out.println(inputHandler.mainMenu());
            userActivity = inputHandler.getInput();

            if(userActivity.equals("Check out")){
                inputHandler.logInPrompt();
                inputHandler.checkOutPrompt(repository);
            } else if(userActivity.equals("Check in")){
                inputHandler.logInPrompt();
                inputHandler.checkInPrompt(repository);
            } else if(userActivity.equals("List books")){
                printBorrowables(repository.getAvailableBookList());
            } else if(userActivity.equals("List movies")){
                printBorrowables(repository.getAvailableMovieList());
            } else if(userActivity.equals("See my information")){
                User user = userManager.getCurrentUser();
                System.out.println(user.getInformation());
            } else {
                System.out.println("Select a valid option.");
            }
        }
    }

    public static void printBorrowables(List<Borrowable> borrowables){
        for(Borrowable borrowable : borrowables) {
            System.out.println(borrowable.getTitle());
        }
    }

    public static Repository buildLibrary(){
        List<Borrowable> borrowables = new ArrayList<Borrowable>();
        borrowables.add(new Book("El Tunel"));
        borrowables.add(new Book("Rayuela"));
        borrowables.add(new Movie("The Matrix", "4", "Juan Díaz", "1987"));
        borrowables.add(new Movie("I Robot", "5", "Alex Pérez", "2004"));
        return new Repository(borrowables);
    }

    public static void createUsers(UserManager userManager) {
        User user = new User("Pamela", "pame@hot.com", "3310093", "222-2222", "***");
        userManager.addUser(user);
    }
}
